package com.zxjbyte.yiyi.framework.operatelog.aspect;

import cn.hutool.core.date.DateUtil;
import com.zxjbyte.yiyi.framework.operatelog.annotation.OperateLog;
import com.zxjbyte.yiyi.framework.operatelog.publisher.OperateLogPublisher;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.Date;

/**
 * 操作日志切面
 * @Author zhangxingjia
 * @Date 2023/3/8 11:10
 * @Version: 1.0
 */
@Slf4j
@Aspect
public class OperateLogAspect {

    @Around("@annotation(operateLog)")
    public Object around(ProceedingJoinPoint joinPoint, OperateLog operateLog) throws Throwable {
        // 执行结果
        Object result = null;
        // 执行时长
        int duration = 0;
        Date now = new Date();
        long preTime = now.getTime();
        try {
            result = joinPoint.proceed();
            duration = (int) DateUtil.spendMs(preTime);
            return result;
        } finally {
            OperateLogPublisher.publishEvent(joinPoint, result, operateLog, now, duration);
        }
    }
}
